package com.shuorikeji.huohuo.ui.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.TjJlAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.TjInfoBean;
import com.shuorikeji.huohuo.bean.TjMainBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.BusinessUtils;
import com.shuorikeji.huohuo.utils.SpacesItemDecoration;
import com.shuorikeji.huohuo.utils.ToastShowUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class TgCenterActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlView;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_je)
    TextView tvJe;
    @BindView(R.id.tv_ls_sy)
    TextView tvLjSy;
    @BindView(R.id.iv_tg_hb)
    ImageView ivTgHb;
    @BindView(R.id.ll_zt_jl)
    LinearLayout llZtJl;
    @BindView(R.id.tv_zt_jl)
    TextView tvZtJl;
    @BindView(R.id.view_zt_jl)
    View viewZtJl;
    @BindView(R.id.ll_jt_jl)
    LinearLayout llJtJl;
    @BindView(R.id.tv_jt_jl)
    TextView tvJtJl;
    @BindView(R.id.view_jt_jl)
    View viewJtJl;

    private int curZtPage;
    private int tabType;
    private List<TjInfoBean> tjJlList = new ArrayList<>();
    private TjJlAdapter tjJlAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_tg_center);
    }

    @Override
    protected void initData() {
        initSmartRefreshLayout();
        initAdapter();
        setJlTab(1);
        curZtPage = 1;
        getZtJl();
        initClick();
    }

    private void initSmartRefreshLayout() {
        srlView.setRefreshHeader(new ClassicsHeader(TgCenterActivity.this));
        srlView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //刷新
                if (tabType == 1) {
                    curZtPage = 1;
                    getZtJl();
                } else {
                    getJtJl();
                }
            }
        });
        srlView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //加载更多
                if (tabType == 1) {
                    curZtPage = curZtPage + 1;
                    getZtJl();
                } else {
                    getJtJl();
                }
            }
        });
    }

    private void initAdapter() {
        rvData.setLayoutManager(new LinearLayoutManager(TgCenterActivity.this, LinearLayoutManager.VERTICAL, false));
        rvData.addItemDecoration(new SpacesItemDecoration(0, 0, 10, 0));
        tjJlAdapter = new TjJlAdapter(TgCenterActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
            }
        });
        rvData.setAdapter(tjJlAdapter);
    }

    /**
     * @param i 1直推记录
     *          2间推记录
     */
    private void setJlTab(int i) {
        tabType = i;
        tvZtJl.setTextColor((i == 1) ? getResources().getColor(R.color.color_FF7100) : getResources().getColor(R.color.color_666666));
        BusinessUtils.setTxtFold(tvZtJl, (i == 1));
        viewZtJl.setVisibility((i == 1) ? View.VISIBLE : View.INVISIBLE);

        tvJtJl.setTextColor((i == 2) ? getResources().getColor(R.color.color_FF7100) : getResources().getColor(R.color.color_666666));
        BusinessUtils.setTxtFold(tvJtJl, (i == 2));
        viewJtJl.setVisibility((i == 2) ? View.VISIBLE : View.INVISIBLE);
    }

    private void getZtJl() {
        mPresenter.ztJl(true, curZtPage, 20, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                srlView.finishLoadMore();
                srlView.finishRefresh();
                switch (flag) {
                    case 1:
                        TjMainBean data = (TjMainBean) object;
                        if (curZtPage == 1) {
                            tjJlList.clear();
                        }
                        tjJlList.addAll(data.getData().getData());
                        tjJlAdapter.setData(tjJlList);
                        if (tjJlList != null && tjJlList.size() > 0) {
                        } else {
                            ToastShowUtil.showToastCenter(TgCenterActivity.this, "暂无数据");
                        }
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(TgCenterActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void getJtJl() {
        mPresenter.jtJl(true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                srlView.finishLoadMore();
                srlView.finishRefresh();
                switch (flag) {
                    case 1:
                        TjMainBean data = (TjMainBean) object;
                        tjJlList.clear();
                        tjJlList.addAll(data.getData().getData());
                        tjJlAdapter.setData(tjJlList);
                        if (tjJlList != null && tjJlList.size() > 0) {
                        } else {
                            ToastShowUtil.showToastCenter(TgCenterActivity.this, "暂无数据");
                        }
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(TgCenterActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
        ivTgHb.setOnClickListener(view -> {
            Intent intent = new Intent(TgCenterActivity.this, TgHbActivity.class);
            startActivity(intent);
        });
        llZtJl.setOnClickListener(view -> {
            setJlTab(1);
            curZtPage = 1;
            getZtJl();
        });
        llJtJl.setOnClickListener(view -> {
            setJlTab(2);
            getJtJl();
        });
    }
}